package com.qf.flash.ui.mine;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.qf.flash.R;
import com.qf.flash.event.GetCashEvent;
import com.qf.flash.model.AccountInfoBean;
import com.qf.flash.model.AlipayInfoBean;
import com.qf.flash.mvp.contract.BusinessInfoContrat;
import com.qf.flash.mvp.presenter.BusinessInfoPresenter;
import com.qf.flash.ui.business.MyBillActivity;
import com.qf.flash.ui.business.OrderHallActivity;
import com.qf.flash.ui.business.SponsorGameActivity;
import com.qf.flashlib.base.BaseActivity;
import com.qf.flashlib.util.ViewUtil;

import org.greenrobot.eventbus.Subscribe;

import java.text.DecimalFormat;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 商家入口activity
 */
public class BusinessInActivity extends BaseActivity implements BusinessInfoContrat.View {
    @BindView(R.id.tv_top_back)
    TextView tvTopBack;
    @BindView(R.id.tv_top_title)
    TextView tvTopTitle;
    @BindView(R.id.tv_top_right)
    TextView tvTopRight;
    @BindView(R.id.tv_tx)
    TextView tvTx;
    @BindView(R.id.tv_unit)
    TextView tvUnit;
    @BindView(R.id.tv_num)
    TextView tvNum;
    @BindView(R.id.ll_my_order)
    LinearLayout llMyOrder;
    @BindView(R.id.ll_order_hall)
    LinearLayout llOrderHall;
    @BindView(R.id.ll_my_code)
    LinearLayout llMyCode;
    @BindView(R.id.ll_bind_zfb)
    LinearLayout llBindZfb;
    private PopupWindow mPopWindow;
    private WindowManager.LayoutParams lp;
    private BusinessInfoPresenter businessInfoPresenter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_business_in;
    }

    @Override
    protected void initInject() {
        super.initInject();
        businessInfoPresenter = new BusinessInfoPresenter();
        initPresenters(businessInfoPresenter);
    }

    @Override
    protected void initViewAndData() {
        tvTopTitle.setText("商家入口");
        ViewUtil.setRightIcon(this, tvTopRight, R.mipmap.nav_more);
        businessInfoPresenter.getBusinessEnterInfo("3");
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @Override
    protected boolean useEvent() {
        return true;
    }

    @Subscribe
    public void onGetCashEvent(GetCashEvent event) {
        businessInfoPresenter.getBusinessEnterInfo("3");
    }

    @OnClick({R.id.tv_top_back, R.id.tv_top_right, R.id.tv_tx, R.id.ll_my_order, R.id.ll_order_hall, R.id.ll_my_code, R.id.ll_bind_zfb})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_top_back:
                finish();
                break;
            case R.id.tv_top_right:
                showPopupWindow();
                break;
            case R.id.tv_tx:
                GetCashActivity.start(this, "1");
                break;
            case R.id.ll_my_order:
                MyBillActivity.start(this);
                break;
            case R.id.ll_order_hall:
                OrderHallActivity.start(this);
                break;
            case R.id.ll_my_code: // 发起活动
                SponsorGameActivity.start(this);
                break;
            case R.id.ll_bind_zfb:
//                BindAlipayActivity.start(this);
                businessInfoPresenter.getAlipayInfo();
                break;
        }
    }

    public static void start(Activity activity) {
        Intent intent = new Intent(activity, BusinessInActivity.class);
        activity.startActivity(intent);
    }

    // popupWindow
    private void showPopupWindow() {
        final View contentView = LayoutInflater.from(BusinessInActivity.this).inflate(R.layout.pop_menu, null);
        mPopWindow = new PopupWindow(contentView);
        mPopWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
        mPopWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);

        TextView tv1 = (TextView) contentView.findViewById(R.id.tv_balance);
        mPopWindow.setOutsideTouchable(true);
        mPopWindow.setFocusable(true);
        //防止点击外面消失不生效
        mPopWindow.setBackgroundDrawable(new ColorDrawable(0x00000000));
        mPopWindow.showAsDropDown(tvTopRight, -120, -24);
        bgAlpha(0.5f);


        tv1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mPopWindow.dismiss();

                MyBalanceDetailActivity.start(BusinessInActivity.this, "3");

            }
        });
        mPopWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                bgAlpha(1f);
            }
        });
    }

    /**
     * 设置背景
     *
     * @param bgAlpha
     */
    private void bgAlpha(float bgAlpha) {
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.alpha = bgAlpha;//0.0-1.0
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        getWindow().setAttributes(lp);
    }

    @Override
    public void setAccountInfo(AccountInfoBean accountInfo) {
        DecimalFormat df = new DecimalFormat("#0.00");
        String money = df.format(Double.parseDouble(accountInfo.getShopMoney()));
        tvNum.setText(money);
    }

    @Override
    public void setAlipayInfoBean(AlipayInfoBean alipayInfoBean) {
        BindAlipayActivity.start(this, alipayInfoBean);
    }

}
